package com.ruoyi.dish.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.dish.Dto.DishDto;
import com.ruoyi.system.api.CategoryServerFeignClient;
import com.ruoyi.system.api.domain.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.dish.domain.DishFlavor;
import com.ruoyi.dish.mapper.DishMapper;
import com.ruoyi.dish.domain.Dish;
import com.ruoyi.dish.service.IDishService;

import javax.annotation.Resource;

/**
 * 菜品管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-21
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements IDishService
{
    @Autowired
    private DishMapper dishMapper;
    @Resource
    CategoryServerFeignClient categoryServerFeignClient;

    /*public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        Long dishId = dishDto.getId();//菜品id
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }*/

    /**
     * 查询菜品管理
     * 
     * @param id 菜品管理主键
     * @return 菜品管理
     */
    @Override
    public Dish selectDishById(Long id)
    {
        return dishMapper.selectDishById(id);
    }

    /**
     * 查询菜品管理列表
     * 
     * @param dish 菜品管理
     * @return 菜品管理
     */
    @Override
    public List<Dish> selectDishList(Dish dish) {
        List<Dish> dishes = dishMapper.selectDishList(dish);
        for(Dish dish1:dishes){
            AjaxResult info = categoryServerFeignClient.getInfo(dish1.getCategoryId());
            Map map= (Map) info.get("data");
            Category category = new Category();
            category.setName((String) map.get("name"));
            dish1.setCategory(category);
        }
        return dishes;
//        return dishMapper.selectDishList(dish);
    }

    /**
     * 新增菜品管理
     * 
     * @param dish 菜品管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDish(Dish dish)
    {
        dish.setStatus(1);
        dish.setCreateTime(DateUtils.getNowDate());
        int rows = dishMapper.insertDish(dish);
        insertDishFlavor(dish);
        return rows;
    }

    /**
     * 修改菜品管理
     * 
     * @param dish 菜品管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDish(Dish dish)
    {
        dish.setUpdateTime(DateUtils.getNowDate());
        dishMapper.deleteDishFlavorByDishId(dish.getId());
        insertDishFlavor(dish);
        return dishMapper.updateDish(dish);
    }

    /**
     * 批量删除菜品管理
     * 
     * @param ids 需要删除的菜品管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDishByIds(Long[] ids)
    {
        dishMapper.deleteDishFlavorByDishIds(ids);
        return dishMapper.deleteDishByIds(ids);
    }

    @Override
    public int updateplts(Long[] ids) {
        return dishMapper.updatePlts(ids);
    }

    @Override
    public int updateplqs(Long[] ids) {
        return dishMapper.updateplqs(ids);
    }

    @Override
    public int updateStatus(Integer ids, Integer status) {
        return dishMapper.updateStatus(ids, status);
    }

    /**
     * 删除菜品管理信息
     * 
     * @param id 菜品管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDishById(Long id){
        dishMapper.deleteDishFlavorByDishId(id);
        return dishMapper.deleteDishById(id);
    }

    @Override
    public List<Dish> getcategoryId(Long id) {
        return dishMapper.getcategoryId(id);
    }

    /**
     * 新增菜品口味关系信息
     * 
     * @param dish 菜品管理对象
     */
    public void insertDishFlavor(Dish dish){
        List<DishFlavor> dishFlavorList = dish.getDishFlavorList();
        Long id = dish.getId();
        if (StringUtils.isNotNull(dishFlavorList)){
            List<DishFlavor> list = new ArrayList<DishFlavor>();
            for (DishFlavor dishFlavor : dishFlavorList){
                dishFlavor.setDishId(id);
                dishFlavor.setCreateTime(DateUtils.getNowDate());
                list.add(dishFlavor);
            }
            if (list.size() > 0){
                dishMapper.batchDishFlavor(list);
            }
        }
    }
}
